package com.lifesea.notify.sms;


import com.alibaba.fastjson.JSON;
import com.lifesea.base.entity.SmsRequest;
import com.lifesea.base.entity.SmsSendResult;
import com.lifesea.commons.utils.HttpUtils;
import com.lifesea.commons.utils.MD5Util;
import com.lifesea.conifg.SmsConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.Clock;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by banxia on 2016/2/25.
 */
@Service
public class ZgjkwSmsSender extends AbstractSmsService {

    private static final Logger logger = LoggerFactory.getLogger(ZgjkwSmsSender.class);
    
    private static final String SMS_EH_OPENID = "129Oedacy9sdsHW123WEW";
    private static final String SMS_EH_METHOD = "ecard.sms.custom";
    private static final String SMS_EH_V = "1.0";

    public SmsSendResult sending(String mobile, String text, String model) {
        Map<String, String> params = new HashMap<>();
        String timestamp = String.valueOf(Clock.systemUTC().millis());
        params.put("openid", SMS_EH_OPENID);
        params.put("method", StringUtils.isEmpty(model) ? SMS_EH_METHOD : model);
        params.put("v", SMS_EH_V);
        params.put("timestamp", timestamp);
        params.put("content", text);
        params.put("mobile", mobile);

        String signString = SMS_EH_OPENID + ";dUd5Rx21HGSG12UQ2;" + SMS_EH_METHOD + ";" + SMS_EH_V + ";" + timestamp;


        for (Map.Entry entry : params.entrySet()) {
            signString += ";" + entry.getKey() + "=" + params.get(entry.getValue());
        }

        params.put("sign", MD5Util.MD5(signString));

        String rest = HttpUtils.doPost(SmsConfig.SMS_EH_URL, params);

        return JSON.parseObject(rest, SmsSendResult.class);

    }

    @Override
    String[] initKey() {
        return new String[]{"*"};
    }
}
